# Copyright 2020 Makani Technologies LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Automatically generated controllers.

This file was generated by: analysis/control/trans_in.py.
"""

from makani.control import control_types as m


def GetControllers(wing_serial):
  """Returns control gain matrices for any kite serial number."""
  if wing_serial == m.kWingSerial01:
    dCL_dflap = (
        1.36
    )
    angle_of_sideslip_cmd = (
        -0.0384
    )
    roll_ti_cmd = (
        0.0405
    )
    yaw_ti_cmd = (
        0.0521
    )
    delta_elevator_alpha_zero = (
        0.0
    )
    ddelta_elevator_dalpha = (
        -0.67
    )
    lat_gains_pitch_forward = (
        [[0.0, 32532.191, 0.0, 47928.314, 0.0, 0.0],
         [0.0, 0.0, -0.384, -0.039, -1.607, 0.0],
         [0.0, 0.0, -0.114, -1.073, -0.584, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 32532.191, 0.0, 47928.314, 0.0, 0.0],
         [-1.742, 0.082, -0.384, -0.039, -1.607, 0.0],
         [-0.655, -0.597, -0.114, -1.073, -0.584, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 29363.629, 0.0, 44618.76, 0.0, 0.0],
         [-1.632, 0.04, -0.362, -0.038, -1.6, 0.0],
         [-0.714, -0.585, -0.095, -1.087, -0.651, 0.0]]
    )
    long_gains_low_tension = (
        [[524.613, 439.011, 398.613], [-0.699, -0.585, -0.531]]
    )
    long_gains_high_tension = (
        [[482.297, 407.586, 372.022], [-0.689, -0.582, -0.531]]
    )
    flap_offsets = (
        [-0.2159, -0.2159, -0.2007, -0.2007, -0.1855, -0.1855, 0.0, -0.0848]
    )

  elif wing_serial == m.kWingSerial04Hover:
    dCL_dflap = (
        1.38
    )
    angle_of_sideslip_cmd = (
        -0.0501
    )
    roll_ti_cmd = (
        0.0212
    )
    yaw_ti_cmd = (
        0.0748
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.61
    )
    lat_gains_pitch_forward = (
        [[0.0, 40349.959, 0.0, 58409.76, 0.0, 0.0],
         [0.0, 0.0, -0.4, -0.051, -1.614, 0.0],
         [0.0, 0.0, -0.092, -0.97, -0.552, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 40349.959, 0.0, 58409.76, 0.0, 0.0],
         [-1.803, 0.097, -0.4, -0.051, -1.614, 0.0],
         [-0.619, -0.507, -0.092, -0.97, -0.552, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 36018.853, 0.0, 53377.965, 0.0, 0.0],
         [-1.673, 0.044, -0.374, -0.049, -1.609, 0.0],
         [-0.686, -0.524, -0.073, -1.009, -0.623, 0.0]]
    )
    long_gains_low_tension = (
        [[676.696, 553.667, 480.853], [-0.746, -0.61, -0.53]]
    )
    long_gains_high_tension = (
        [[583.805, 483.186, 426.219], [-0.727, -0.601, -0.531]]
    )
    flap_offsets = (
        [-0.217, -0.217, -0.2007, -0.2007, -0.1844, -0.1844, 0.0, -0.09]
    )

  elif wing_serial == m.kWingSerial04Crosswind:
    dCL_dflap = (
        1.36
    )
    angle_of_sideslip_cmd = (
        -0.0485
    )
    roll_ti_cmd = (
        0.0228
    )
    yaw_ti_cmd = (
        0.0712
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.59
    )
    lat_gains_pitch_forward = (
        [[0.0, 33116.547, 0.0, 49458.918, 0.0, 0.0],
         [0.0, 0.0, -0.379, -0.036, -1.607, 0.0],
         [0.0, 0.0, -0.111, -1.06, -0.577, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 33116.547, 0.0, 49458.918, 0.0, 0.0],
         [-1.739, 0.092, -0.379, -0.036, -1.607, 0.0],
         [-0.647, -0.574, -0.111, -1.06, -0.577, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 30216.531, 0.0, 46166.051, 0.0, 0.0],
         [-1.63, 0.049, -0.357, -0.035, -1.599, 0.0],
         [-0.712, -0.57, -0.092, -1.077, -0.644, 0.0]]
    )
    long_gains_low_tension = (
        [[571.243, 464.509, 406.696], [-0.746, -0.606, -0.531]]
    )
    long_gains_high_tension = (
        [[528.334, 430.953, 380.028], [-0.738, -0.602, -0.531]]
    )
    flap_offsets = (
        [-0.219, -0.219, -0.2007, -0.2007, -0.1824, -0.1824, 0.0, -0.086]
    )

  elif wing_serial == m.kWingSerial05Hover:
    dCL_dflap = (
        1.37
    )
    angle_of_sideslip_cmd = (
        -0.0477
    )
    roll_ti_cmd = (
        0.0229
    )
    yaw_ti_cmd = (
        0.0706
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.59
    )
    lat_gains_pitch_forward = (
        [[0.0, 37923.226, 0.0, 55200.308, 0.0, 0.0],
         [0.0, 0.0, -0.386, -0.046, -1.61, 0.0],
         [0.0, 0.0, -0.096, -0.99, -0.567, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 37923.226, 0.0, 55200.308, 0.0, 0.0],
         [-1.778, 0.098, -0.386, -0.046, -1.61, 0.0],
         [-0.633, -0.526, -0.096, -0.99, -0.567, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 33864.615, 0.0, 50651.941, 0.0, 0.0],
         [-1.653, 0.048, -0.362, -0.044, -1.604, 0.0],
         [-0.699, -0.533, -0.077, -1.021, -0.639, 0.0]]
    )
    long_gains_low_tension = (
        [[649.137, 528.469, 462.501], [-0.744, -0.606, -0.53]]
    )
    long_gains_high_tension = (
        [[566.706, 469.253, 416.505], [-0.722, -0.598, -0.531]]
    )
    flap_offsets = (
        [-0.2147, -0.2147, -0.2007, -0.2007, -0.1867, -0.1867, 0.0, -0.0852]
    )

  elif wing_serial == m.kWingSerial05Crosswind:
    dCL_dflap = (
        1.36
    )
    angle_of_sideslip_cmd = (
        -0.0437
    )
    roll_ti_cmd = (
        0.0296
    )
    yaw_ti_cmd = (
        0.0622
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.57
    )
    lat_gains_pitch_forward = (
        [[0.0, 31189.817, 0.0, 46624.658, 0.0, 0.0],
         [0.0, 0.0, -0.368, -0.031, -1.604, 0.0],
         [0.0, 0.0, -0.112, -1.076, -0.588, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 31189.817, 0.0, 46624.658, 0.0, 0.0],
         [-1.719, 0.094, -0.368, -0.031, -1.604, 0.0],
         [-0.656, -0.591, -0.112, -1.076, -0.588, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 28224.47, 0.0, 43201.405, 0.0, 0.0],
         [-1.612, 0.05, -0.347, -0.031, -1.595, 0.0],
         [-0.723, -0.588, -0.095, -1.095, -0.657, 0.0]]
    )
    long_gains_low_tension = (
        [[553.034, 448.305, 395.211], [-0.743, -0.602, -0.531]]
    )
    long_gains_high_tension = (
        [[506.687, 412.274, 365.877], [-0.736, -0.599, -0.531]]
    )
    flap_offsets = (
        [-0.216, -0.216, -0.2007, -0.2007, -0.1854, -0.1854, 0.0, -0.0815]
    )

  elif wing_serial == m.kWingSerial06Hover:
    dCL_dflap = (
        1.38
    )
    angle_of_sideslip_cmd = (
        -0.0499
    )
    roll_ti_cmd = (
        0.0205
    )
    yaw_ti_cmd = (
        0.0747
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.59
    )
    lat_gains_pitch_forward = (
        [[0.0, 40512.573, 0.0, 58630.073, 0.0, 0.0],
         [0.0, 0.0, -0.4, -0.05, -1.613, 0.0],
         [0.0, 0.0, -0.092, -0.97, -0.552, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 40512.573, 0.0, 58630.073, 0.0, 0.0],
         [-1.803, 0.1, -0.4, -0.05, -1.613, 0.0],
         [-0.62, -0.506, -0.092, -0.97, -0.552, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 36005.029, 0.0, 53484.98, 0.0, 0.0],
         [-1.671, 0.046, -0.374, -0.049, -1.608, 0.0],
         [-0.686, -0.522, -0.073, -1.009, -0.623, 0.0]]
    )
    long_gains_low_tension = (
        [[683.186, 556.273, 481.947], [-0.751, -0.611, -0.53]]
    )
    long_gains_high_tension = (
        [[587.799, 484.672, 426.751], [-0.731, -0.603, -0.531]]
    )
    flap_offsets = (
        [-0.2156, -0.2156, -0.2007, -0.2007, -0.1858, -0.1858, 0.0, -0.0878]
    )

  elif wing_serial == m.kWingSerial06Crosswind:
    dCL_dflap = (
        1.36
    )
    angle_of_sideslip_cmd = (
        -0.0453
    )
    roll_ti_cmd = (
        0.0272
    )
    yaw_ti_cmd = (
        0.0654
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.56
    )
    lat_gains_pitch_forward = (
        [[0.0, 33145.549, 0.0, 49466.023, 0.0, 0.0],
         [0.0, 0.0, -0.378, -0.035, -1.606, 0.0],
         [0.0, 0.0, -0.111, -1.061, -0.577, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 33145.549, 0.0, 49466.023, 0.0, 0.0],
         [-1.737, 0.094, -0.378, -0.035, -1.606, 0.0],
         [-0.647, -0.576, -0.111, -1.061, -0.577, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 30189.447, 0.0, 46159.2, 0.0, 0.0],
         [-1.629, 0.05, -0.356, -0.035, -1.598, 0.0],
         [-0.712, -0.57, -0.093, -1.077, -0.644, 0.0]]
    )
    long_gains_low_tension = (
        [[573.181, 464.571, 406.364], [-0.749, -0.607, -0.531]]
    )
    long_gains_high_tension = (
        [[529.675, 430.508, 379.301], [-0.742, -0.603, -0.531]]
    )
    flap_offsets = (
        [-0.2169, -0.2169, -0.2007, -0.2007, -0.1845, -0.1845, 0.0, -0.0837]
    )

  elif wing_serial == m.kWingSerial07Hover:
    dCL_dflap = (
        1.38
    )
    angle_of_sideslip_cmd = (
        -0.0501
    )
    roll_ti_cmd = (
        0.0212
    )
    yaw_ti_cmd = (
        0.0748
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.61
    )
    lat_gains_pitch_forward = (
        [[0.0, 40349.959, 0.0, 58409.76, 0.0, 0.0],
         [0.0, 0.0, -0.4, -0.051, -1.614, 0.0],
         [0.0, 0.0, -0.092, -0.97, -0.552, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 40349.959, 0.0, 58409.76, 0.0, 0.0],
         [-1.803, 0.097, -0.4, -0.051, -1.614, 0.0],
         [-0.619, -0.507, -0.092, -0.97, -0.552, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 36018.853, 0.0, 53377.965, 0.0, 0.0],
         [-1.673, 0.044, -0.374, -0.049, -1.609, 0.0],
         [-0.686, -0.524, -0.073, -1.009, -0.623, 0.0]]
    )
    long_gains_low_tension = (
        [[676.696, 553.667, 480.853], [-0.746, -0.61, -0.53]]
    )
    long_gains_high_tension = (
        [[583.805, 483.186, 426.219], [-0.727, -0.601, -0.531]]
    )
    flap_offsets = (
        [-0.217, -0.217, -0.2007, -0.2007, -0.1844, -0.1844, 0.0, -0.09]
    )

  elif wing_serial == m.kWingSerial07Crosswind:
    dCL_dflap = (
        1.36
    )
    angle_of_sideslip_cmd = (
        -0.0485
    )
    roll_ti_cmd = (
        0.0228
    )
    yaw_ti_cmd = (
        0.0712
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.59
    )
    lat_gains_pitch_forward = (
        [[0.0, 33116.547, 0.0, 49458.918, 0.0, 0.0],
         [0.0, 0.0, -0.379, -0.036, -1.607, 0.0],
         [0.0, 0.0, -0.111, -1.06, -0.577, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 33116.547, 0.0, 49458.918, 0.0, 0.0],
         [-1.739, 0.092, -0.379, -0.036, -1.607, 0.0],
         [-0.647, -0.574, -0.111, -1.06, -0.577, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 30216.531, 0.0, 46166.051, 0.0, 0.0],
         [-1.63, 0.049, -0.357, -0.035, -1.599, 0.0],
         [-0.712, -0.57, -0.092, -1.077, -0.644, 0.0]]
    )
    long_gains_low_tension = (
        [[571.243, 464.509, 406.696], [-0.746, -0.606, -0.531]]
    )
    long_gains_high_tension = (
        [[528.334, 430.953, 380.028], [-0.738, -0.602, -0.531]]
    )
    flap_offsets = (
        [-0.219, -0.219, -0.2007, -0.2007, -0.1824, -0.1824, 0.0, -0.086]
    )

  # TODO: OktKite. Migrate the OktoberKite trans-in gains to oktoberkite
  # configuration file at a later time. This will break if the gains are
  # recomputed without an additional copy/paste into this file.
  # These gains are based on kWingSerial04Crosswind in this file.
  elif wing_serial == m.kWingSerialOktoberKite01:
    dCL_dflap = (
        1.07
    )
    angle_of_sideslip_cmd = (
        -0.0473
    )
    roll_ti_cmd = (
        0.0246
    )
    yaw_ti_cmd = (
        0.0689
    )
    delta_elevator_alpha_zero = (
        0.02
    )
    ddelta_elevator_dalpha = (
        -0.57
    )
    lat_gains_pitch_forward = (
        [[0.0, 32955.099, 0.0, 49230.506, 0.0, 0.0],
         [0.0, 0.0, -0.377, -0.035, -1.607, 0.0],
         [0.0, 0.0, -0.112, -1.063, -0.576, 0.0]]
    )
    lat_gains_low_tension = (
        [[0.0, 32955.099, 0.0, 49230.506, 0.0, 0.0],
         [-1.735, 0.092, -0.377, -0.035, -1.607, 0.0],
         [-0.646, -0.577, -0.112, -1.063, -0.576, 0.0]]
    )
    lat_gains_high_tension = (
        [[0.0, 29772.254, 0.0, 45737.486, 0.0, 0.0],
         [-1.625, 0.049, -0.355, -0.034, -1.598, 0.0],
         [-0.71, -0.57, -0.094, -1.081, -0.645, 0.0]]
    )
    long_gains_low_tension = (
        [[566.74, 460.625, 403.495], [-0.746, -0.606, -0.531]]
    )
    long_gains_high_tension = (
        [[522.074, 426.079, 375.988], [-0.738, -0.602, -0.531]]
    )
    flap_offsets = (
        [-0.2187, -0.2187, -0.2007, -0.2007, -0.1827, -0.1827, 0.0, -0.0855]
    )

  else:
    assert False, 'wing_serial %d was not recognized' % wing_serial

  return {
      'dCL_dflap': (
          dCL_dflap),
      'angle_of_sideslip_cmd': (
          angle_of_sideslip_cmd),
      'roll_ti_cmd': (
          roll_ti_cmd),
      'yaw_ti_cmd': (
          yaw_ti_cmd),
      'delta_elevator_alpha_zero': (
          delta_elevator_alpha_zero),
      'ddelta_elevator_dalpha': (
          ddelta_elevator_dalpha),
      'lat_gains_pitch_forward': (
          lat_gains_pitch_forward),
      'lat_gains_low_tension': (
          lat_gains_low_tension),
      'lat_gains_high_tension': (
          lat_gains_high_tension),
      'long_gains_low_tension': (
          long_gains_low_tension),
      'long_gains_high_tension': (
          long_gains_high_tension),
      'flap_offsets': (
          flap_offsets),
  }
